<?php

/**
 * manage contact form and reply email
 */
class ContactusController extends Controller {
    public $activeMenu = 'contact';
    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id) {
        $model = $this->loadModel($id);

        if(!$model->readed){
            $model->readed = ContactUs::READED;

            $model->save();
        }

        $this->render('view', array(
            'model' => $model,
        ));
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id) {
        if (Yii::app()->request->isPostRequest) {
            // we only allow deletion via POST request
            $this->loadModel($id)->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    /**
     * Manages all models.
     */
    public function actionIndex() {
        $model = new ContactUs('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['ContactUs']))
            $model->attributes = $_GET['ContactUs'];

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id) {
        $model = ContactUs::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');

        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'contact-us-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    /**
     * send email to user
     */
    public function actionSend(){
        $model = new SendMailForm();

        if(isset($_GET['email'])){
            $model->email = $_GET['email'];
        }

        if(r()->isPostRequest){
            $model->attributes = $_POST['SendMailForm'];

            //validate model and send email
            if($model->validate()){
                //send email to client
                EmailHelper::send($model->email, $model->subject, $model->body);

                user()->setFlash('success', 'Email has been sent');

                $this->refresh();
            }
        }

        $this->render('send', array(
            'model' => $model
        ));
    }
}
